package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.entity.PracticeSubjectsType;
import com.xmy.cultivate.entity.PracticeType;
import com.xmy.cultivate.entity.Quarter;
import com.xmy.cultivate.entity.SchoolQuarter;
import com.xmy.cultivate.entity.response.PracticePlanStaff;
import com.xmy.cultivate.mapper.PracticeSubjectsTypeMapper;
import com.xmy.cultivate.service.IPracticeSubjectsTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.service.IPracticeTypeService;
import com.xmy.cultivate.service.IQuarterService;
import com.xmy.cultivate.service.ISchoolQuarterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 科目类型 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-10-09
 */
@Service
public class PracticeSubjectsTypeServiceImpl extends ServiceImpl<PracticeSubjectsTypeMapper, PracticeSubjectsType> implements IPracticeSubjectsTypeService {

    @Autowired
    PracticeSubjectsTypeMapper practiceSubjectsTypeMapper;

    @Autowired
    ISchoolQuarterService iSchoolQuarterService;

    @Autowired
    IQuarterService iQuarterService;

    @Autowired
    IPracticeTypeService iPracticeTypeService;

    @Override
    public List<PracticeSubjectsType> getPracticeSubjectsTypeList(Wrapper wrapper) {
        return practiceSubjectsTypeMapper.getPracticeSubjectsTypeList(wrapper);
    }

    /**
     * 完成率列表
     */
    @Override
    public List<PracticePlanStaff> practicePlanStaffList(String type, String practiceTypeId, String yearPart, String quarterNum, String subjectsId, String startDate, String endDate, String lessonNumStart, String lessonNumEnd, String lessonType, String schoolId, String yearClassId, String classTypeId, String teacherName, String classify, String trackType, String teamId, String trackId) {
        Quarter quarter = iQuarterService.getNowQuarter();
        if (StringUtils.isBlank(yearPart)) {
            yearPart = String.valueOf(quarter.getYearPart());
        }
        if (StringUtils.isBlank(quarterNum)) {
            quarterNum = String.valueOf(quarter.getNum());
        }

        List<PracticePlanStaff> practiceSubjectsTypeList = new ArrayList<>();

        //PracticeType practiceType = iPracticeTypeService.getById(practiceTypeId);
        Integer teacherType = null;//已不需要此字段

        if (type.equals("5")) {
            practiceSubjectsTypeList = practiceSubjectsTypeMapper.practicePlanStaffTeamList(practiceTypeId, yearPart, quarterNum, subjectsId, startDate, endDate, lessonNumStart, lessonNumEnd, lessonType, yearClassId, classTypeId, teacherName, classify, trackType, teamId, teacherType,trackId);
        } else {
            practiceSubjectsTypeList = practiceSubjectsTypeMapper.practicePlanStaffList(type, practiceTypeId, yearPart, quarterNum, subjectsId, startDate, endDate, lessonNumStart, lessonNumEnd, lessonType, schoolId, yearClassId, classTypeId, teacherName, teacherType, trackId);

            if (type.equals("6") && StringUtils.isBlank(schoolId)) {
                List<PracticePlanStaff> collectList = new ArrayList<>();

                QueryWrapper queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("year_part", yearPart);
                queryWrapper.eq("quarter_num", quarterNum);
                queryWrapper.eq("pid", 0);
                List<SchoolQuarter> schoolQuarterList = iSchoolQuarterService.list(queryWrapper);

                for (SchoolQuarter schoolQuarter : schoolQuarterList) {
                    PracticePlanStaff practicePlanStaff = new PracticePlanStaff();
                    practicePlanStaff.setSchoolId(schoolQuarter.getSchoolId());
                    practicePlanStaff.setSchoolName(schoolQuarter.getSchoolName());
                    practicePlanStaff.setSchoolPid(schoolQuarter.getPid());
                    practicePlanStaff.setShowPassNum(0);
                    practicePlanStaff.setYetPassNum(0);
                    practicePlanStaff.setNotPassNum(0);
                    practicePlanStaff.setAllScore(0);

                    collectList.add(practicePlanStaff);
                }

                for (PracticePlanStaff practicePlanStaff : collectList) {
                    List<PracticePlanStaff> childRates = new ArrayList<>();
                    for (PracticePlanStaff planStaff : practiceSubjectsTypeList) {
                        if (practicePlanStaff.getSchoolId().equals(planStaff.getSchoolId()) || practicePlanStaff.getSchoolId().equals(planStaff.getSchoolPid())) {
                            childRates.add(planStaff);
                            if (planStaff.getShowPassNum() != null) {
                                practicePlanStaff.setShowPassNum(practicePlanStaff.getShowPassNum() + planStaff.getShowPassNum());
                            }
                            if (planStaff.getYetPassNum() != null) {
                                practicePlanStaff.setYetPassNum(practicePlanStaff.getYetPassNum() + planStaff.getYetPassNum());
                            }
                            if (planStaff.getNotPassNum() != null) {
                                practicePlanStaff.setNotPassNum(practicePlanStaff.getNotPassNum() + planStaff.getNotPassNum());
                            }
                            if (planStaff.getAllScore() != null) {
                                practicePlanStaff.setAllScore(practicePlanStaff.getAllScore() + planStaff.getAllScore());
                            }
                        }
                    }
                    practicePlanStaff.setChildRateList(childRates);
                }

                practiceSubjectsTypeList = collectList;
            }
        }

        return practiceSubjectsTypeList;
    }

    /**
     * 完成率详情
     */
    @Override
    public List<PracticePlanStaff> practicePlanStaffDetails(String type, String practiceTypeId, String yearPart, String quarterNum, String subjectsId, String startDate, String endDate, String lessonNumStart, String lessonNumEnd, String lessonType, String schoolId, String yearClassId, String classTypeId, String teacherId, String detailType, String classify, String trackType, String teamId) {
        Quarter quarter = iQuarterService.getNowQuarter();
        if (StringUtils.isBlank(yearPart)) {
            yearPart = String.valueOf(quarter.getYearPart());
        }
        if (StringUtils.isBlank(quarterNum)) {
            quarterNum = String.valueOf(quarter.getNum());
        }
        //PracticeType practiceType = iPracticeTypeService.getById(practiceTypeId);
        //Integer teacherType = practiceType.getTeacherType();
        Integer teacherType = null;

        List<PracticePlanStaff> practiceSubjectsTypeList = new ArrayList<>();

        if (type.equals("5")) {
            practiceSubjectsTypeList = practiceSubjectsTypeMapper.practicePlanStaffTeamDetails(practiceTypeId, yearPart, quarterNum, subjectsId, startDate, endDate, lessonNumStart, lessonNumEnd, lessonType, schoolId, yearClassId, classTypeId, teacherId, detailType, classify, trackType, teamId, teacherType);
        } else {
            practiceSubjectsTypeList = practiceSubjectsTypeMapper.practicePlanStaffDetails(type, practiceTypeId, yearPart, quarterNum, subjectsId, startDate, endDate, lessonNumStart, lessonNumEnd, lessonType, schoolId, yearClassId, classTypeId, teacherId, detailType, teacherType);
        }

        return practiceSubjectsTypeList;
    }

    @Override
    public List<PracticePlanStaff> practicePlanStaffAllList(String type,
                                                            String practiceTypeId,
                                                            String yearPart,
                                                            String quarterNum,
                                                            String subjectsId,
                                                            String startDate,
                                                            String endDate,
                                                            String lessonNumStart,
                                                            String lessonNumEnd,
                                                            String lessonType,
                                                            String schoolId,
                                                            String yearClassId,
                                                            String classTypeId,
                                                            String teacherName) {
        return practiceSubjectsTypeMapper.practicePlanStaffAllList(type, practiceTypeId, yearPart, quarterNum, subjectsId, startDate, endDate, lessonNumStart, lessonNumEnd, lessonType, schoolId, yearClassId, classTypeId, teacherName);
    }

    @Override
    public IPage<PracticePlanStaff> practicePlanStaffAllDetail(
            IPage<?> page,
            String type,
            String practiceTypeId,
            String yearPart,
            String quarterNum,
            String subjectsId,
            String startDate,
            String endDate,
            String lessonNumStart,
            String lessonNumEnd,
            String lessonType,
            String schoolId,
            String yearClassId,
            String classTypeId,
            String teacherId) {
        return practiceSubjectsTypeMapper.practicePlanStaffAllDetail(page, type, practiceTypeId, yearPart, quarterNum, subjectsId, startDate, endDate, lessonNumStart, lessonNumEnd, lessonType, schoolId, yearClassId, classTypeId, teacherId);
    }
}
